package com.lovely602.langchain4j.stream.controller;


import com.lovely602.langchain4j.stream.service.ChatAssistant;
import com.lovely602.langchain4j.stream.service.StreamingChatAssistant;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 *
 * @author lizhixing
 */
@Slf4j
@RestController
@RequestMapping("/stream")
public class StreamChatController {

    @Autowired //直接使用 low-level LLM API
    private StreamingChatAssistant streamingChatAssistant;

    @Autowired //自己封装接口使用 high-level LLM API
    private StreamingChatModel streamingChatModel;

    @Resource
    private ChatAssistant chatAssistant;

    @Resource
    private ChatModel chatModel;

    @GetMapping(value = "/chat01")
    public String chat01(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {
        String result = "";
        try {
            result = chatAssistant.chat(msg);
        } catch (Exception e) {
            log.error("error:", e);
        }

        return result;
    }

    @GetMapping(value = "/chat02", produces = "text/html;charset=UTF-8")
    public Flux<String> chat02(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {
        return streamingChatAssistant.chatFlux(msg);
    }

    @GetMapping(value = "/chat03", produces = "text/html;charset=UTF-8")
    public Flux<String> chat03(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {

        return Flux.create(emitter -> {
            streamingChatModel.chat(msg, new StreamingChatResponseHandler() {

                @Override
                public void onPartialResponse(String partialResponse) {
                    emitter.next(partialResponse);
                }

                @Override
                public void onCompleteResponse(ChatResponse completeResponse) {
                    emitter.complete();
                }

                @Override
                public void onError(Throwable error) {
                    emitter.error(error);
                }
            });
        });

    }

    @GetMapping(value = "/chat04")
    public void chat04(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {

        streamingChatModel.chat(msg, new StreamingChatResponseHandler() {

            @Override
            public void onPartialResponse(String partialResponse) {
                log.info("partialResponse:{}", partialResponse);
            }

            @Override
            public void onCompleteResponse(ChatResponse completeResponse) {
                log.info("completeResponse:{}", completeResponse);
            }

            @Override
            public void onError(Throwable error) {
                log.error("error:", error);
            }
        });

    }

}
